package com.example.wang_yi_homework3;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    TextView activity_text_view;
    Button button_3, button_1, button_2, button_5, button_4, button_7, button_8;

    private class Loading_Thread extends Thread {

        ProgressBar progressBar;
        TextView percent, how_many;

        public Loading_Thread(ProgressBar progressBar, TextView percent, TextView how_many) {
            this.progressBar = progressBar;
            this.percent = percent;
            this.how_many = how_many;
        }

        @Override
        public void run() {
            for (int j = 0; j <= 100; j++) {
                int finalJ = j;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        progressBar.setProgress(finalJ);
                        percent.setText(finalJ + "%");
                        how_many.setText(finalJ + "");
                    }
                });
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button_3 = findViewById(R.id.button_3);
        activity_text_view = findViewById(R.id.activity_text_view);
        button_1 = findViewById(R.id.button_1);
        button_2 = findViewById(R.id.button_2);
        button_5 = findViewById(R.id.button_5);
        button_4 = findViewById(R.id.button_4);
        button_7 = findViewById(R.id.button_7);
        button_8 = findViewById(R.id.button_8);

        button_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                activity_text_view.setText("匿名内部类作为监听器");
            }
        });

        button_1.setOnClickListener(this);

        button_2.setOnClickListener(new Internally_Click_Listener());

        button_5.setOnClickListener(new Exterior(activity_text_view));

        button_4.setOnClickListener((View view) -> {
            activity_text_view.setText("LAMBDA表达式作为监听器");
        });

        button_7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, ConfigurationTest.class);
                startActivity(intent);
            }
        });

        button_8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Dialog dialog = new Dialog(view.getContext());
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog.setContentView(R.layout.progress_dialog);

                TextView percent = dialog.findViewById(R.id.percent);
                TextView how_many = dialog.findViewById(R.id.how_many);
                ProgressBar progressBar = dialog.findViewById(R.id.progressBar);
                progressBar.setMax(100);
                Loading_Thread loadingThread = new Loading_Thread(progressBar, percent, how_many);
                loadingThread.start();

                dialog.show();
                dialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                dialog.getWindow().setGravity(Gravity.CENTER);
            }
        });
    }

    public void ClickHere(View source)
    {
        activity_text_view.setText("监听器到标签");
    }

    @Override
    public void onClick(View view) {
        activity_text_view.setText("ACTIVITY作为监听器");
    }

    class Internally_Click_Listener implements View.OnClickListener{

        @Override
        public void onClick(View v){
            activity_text_view.setText("内部类作为监听器");
        }
    }
}